package com.gx.smart.building.ui.assets.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.base.BaseViewModel
import com.gx.smart.building.AssetDetailApiService
import com.gx.smart.building.AssetManageApiService
import com.gx.smart.building.AssetTypeApiService
import com.gx.smart.building.OperateStatusApiService
import com.gx.smart.building.PlanTaskApiService
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.device.operatestatus.v1.OperateStatusResp
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.device.operatestatus.v1.Status
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetDetailResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetDto
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetFileDto
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetItemResultDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetDetailQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetListQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetTypeQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.PlanRecordDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.RecordQueryResp

/**
 *@author: xiaosy
 *时间: 2022/4/7
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class AssetsViewModel(app: Application) : BaseViewModel(app) {
    private val tree: MutableLiveData<List<AssetTypeDto>> = MutableLiveData()
    private val list: MutableLiveData<List<AssetDto>> = MutableLiveData()
    private val inspectionList: MutableLiveData<List<PlanRecordDto>> = MutableLiveData()
    private val maintainList: MutableLiveData<List<PlanRecordDto>> = MutableLiveData()
    private val runnableList: MutableLiveData<List<Status>> = MutableLiveData()
    private val detail: MutableLiveData<AssetDto> = MutableLiveData()

    val assetDetail: MutableLiveData<PlanTaskAssetDto> = MutableLiveData()
    val detailFileList: MutableLiveData<List<PlanTaskAssetFileDto>> = MutableLiveData()
    val detailResultList: MutableLiveData<List<PlanTaskAssetItemResultDto>> =
        MutableLiveData()

    //查询资产类型树
    fun queryAssetTypeTree(): LiveData<List<AssetTypeDto>> {
        AssetTypeApiService.getInstance()
            .queryAssetTypeTree(object : CallBack<AssetTypeQueryResp>() {
                override fun callBack(result: AssetTypeQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        tree.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return tree
    }

    //资产列表查询——默认不查库存中的资产
    fun queryAssetList(assetTypeId: String): LiveData<List<AssetDto>> {
        AssetManageApiService.getInstance()
            .queryAssetList(assetTypeId, object : CallBack<AssetListQueryResp>() {
                override fun callBack(result: AssetListQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        list.value = result.assetInfoListList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return list
    }

    //资产列表查询——默认不查库存中的资产
    fun queryAssetListByKeyword(keyword: String): LiveData<List<AssetDto>> {
        AssetManageApiService.getInstance()
            .queryAssetListByKeyword(keyword, object : CallBack<AssetListQueryResp>() {
                override fun callBack(result: AssetListQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        list.value = result.assetInfoListList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return list
    }

    //查询巡检记录
    fun queryInspectionRecordList(
        currentPage: Int,
        assetId: String
    ): LiveData<List<PlanRecordDto>> {
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        AssetDetailApiService.getInstance()
            .queryInspectionRecordList(comQuery, assetId, object : CallBack<RecordQueryResp>() {
                override fun callBack(result: RecordQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        inspectionList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }

            })
        return inspectionList
    }

    //查询保养记录
    fun queryMaintenanceRecordList(
        currentPage: Int,
        assetId: String
    ): LiveData<List<PlanRecordDto>> {
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        AssetDetailApiService.getInstance()
            .queryMaintenanceRecordList(comQuery, assetId, object : CallBack<RecordQueryResp>() {
                override fun callBack(result: RecordQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        maintainList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return maintainList
    }

    //设备当前运行状态查询
    fun queryDeviceOperateStatus(deviceId: String): LiveData<List<Status>> {
        OperateStatusApiService.getInstance()
            .queryDeviceOperateStatus(deviceId, object : CallBack<OperateStatusResp>() {
                override fun callBack(result: OperateStatusResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        runnableList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return runnableList
    }

    //资产详情查询
    fun queryAssetDetail(assetId: String, userId: String): LiveData<AssetDto> {
        AssetManageApiService.getInstance()
            .queryAssetDetail(assetId, userId, object : CallBack<AssetDetailQueryResp>() {
                override fun callBack(result: AssetDetailQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        detail.value = result.assetInfo
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return detail
    }

    /**
     * 移动端任务详情
     */
    fun queryPlanTaskAssetDetail(planTaskId: String, assetId: String) {
        PlanTaskApiService.getInstance()
            .queryPlanTaskAssetDetail(
                planTaskId,
                assetId,
                object : CallBack<QueryPlanTaskAssetDetailResp?>() {
                    override fun callBack(result: QueryPlanTaskAssetDetailResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            //计划任务列表
                            assetDetail.value = result.planTaskAssetDto
                            //处理结果文件
                            detailFileList.value = result.taskAssetFileDtoList
                            detailResultList.value = result.taskAssetItemResultDtoList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
    }

}